import { getToken } from '@/utils/myAuth'
import { findAllBrand } from '@/api/product/brand'
import { AllCategory } from '@/api/category'
import { v4 as uuidv4 } from 'uuid'

export default {
  data() {
    return {
      total: 0,
      start: 1,
      limit: 10,
      BrandList: [],
      uploadFileOss: 'http://leju.bufan.cloud/lejuAdmin/material/uploadFileOss',
      categoryItems: [],
      lejuList: null,
      tempId: '' // 临时id uuid
    }
  },
  cerated() {

  },

  methods: {
    async getBrandList() {
      const res = await findAllBrand()
      console.log(res)
      const { success, data: { items }} = res
      if (success) {
        this.BrandList = items
        console.log(this.BrandList)
      } else {
        this.$mesage.console.error('获取失败')
      }
    },
    async getAllCategory() {
      const res = await AllCategory()
      const { success, data: { items }} = res
      console.log(items)
      if (success) {
        this.lejuList = items.find(item => item.category.name === '乐居').children
        // 手动设置数组对象的格式
        items.forEach(item => {
          this.categoryItems.push({
            ...item.category,
            children: item.children
          })
        })
      }
      console.log(this.categoryItems)
      // console.log(res)
    },
    // 判断图片格式大小
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png' || 'image/webp'
      const isLtSize = file.size / 1024 / 1024 < 1.5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG/WEBP 格式!')
      }
      if (!isLtSize) {
        this.$message.error('上传头像图片大小不能超过 1.5MB!')
      }
      return isJPG && isLtSize
    },

    // 生成sku列表杨式
    buildSkuList() {
      this.$confirm('此操作将删除sku列表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 生成
        this.pmsSkuStockList = []
        this.doBuildSkuList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    doBuildSkuList() {
      const temp1 = this.tempSkuColor.checkdeList
      const temp2 = this.tempSkuSize.checkdeList
      console.log(temp1, temp2)
      console.log(this.pmsSkuStockList)
      if (temp1.length && temp2.length) {
        // const cs = this.pmsSkuStockList.forEach(cItem => {
        //   cItem.color !== temp1 && cItem.size !== temp2
        // })
        const l = true
        if (l) {
          temp1.forEach(item1 => {
            temp2.forEach(item2 => {
              this.pmsSkuStockList.push({
                tempId: uuidv4(),
                'lockStock': '', // 锁定库存
                'lowStock': '', // 库存警戒线
                'pic': '', // 图片
                'price': '', // 当前价格
                'promotionPrice': '', // 单品促销价格
                'sale': '', // 销量
                'skuCode': '', // sku的编码,手动填写
                // spData:  [{"key":"颜色","value":"蓝色"},{"key":"容量","value":"32G"}]
                // 'spData': 'string', // sku属性，json格式String类型 ,
                'stock': '', // 库存
                color: item1,
                size: item2
              })
            })
          })
        }
      } else {
        this.$message.warning('颜色或者大小至少选择一项')
      }
    },
    // sku列表图片
    handleSkuPic(res) {
      const item = this.pmsSkuStockList.find(item => item.id === this.tempId)
      if (item) {
        const { data: { fileUrl }, success } = res
        if (success) {
          item.pic = fileUrl
          // 清空tempId
          this.tempId = ''
        } else {
          this.$message.warning('添加失败')
        }
      } else {
        console.log(res)
      }
    },
    // 获取临时id
    handlPicUpload(tempId, item) {
      if (tempId) {
        this.tempId = tempId
      } else {
        this.tempId = item.id
      }
    },
    // 新增sku
    newAddSkList() {
      this.doBuildSkuList()
    }

  },
  computed: {
    token() {
      return { token: getToken() }
    }
  }
}
